﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using FastReport.Design;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Input;
using 无人值守地磅称重系统.Dialogs;
using 无人值守地磅称重系统.Interfaces;
using 无人值守地磅称重系统.Views;
using CompanyEntiey = Company.Sqlite.Models.Company;

namespace 无人值守地磅称重系统.ViewModels
{
    internal class ReportViewModel : ObservableObject
    {
        private List<Data> dataList = new List<Data>();
        public List<Data> DataList { get => dataList; set => SetProperty(ref dataList, value); }

        private DateTime startDate = DateTime.Now;
        public DateTime StartDate { get => startDate; set => SetProperty(ref startDate, value); }

        private DateTime endDate = DateTime.Now;
        public DateTime EndDate { get => endDate; set=>SetProperty(ref endDate, value); }

        private bool isSearchCar;
        public bool IsSearchCar { get => isSearchCar;set=>SetProperty(ref isSearchCar, value); }

        private bool isSearchStuff;
        public bool IsSearchStuff { get => isSearchStuff; set => SetProperty(ref isSearchStuff, value); }

        private bool isSearchReceiveCompany;
        public bool IsSearchReceiveCompany { get => isSearchReceiveCompany; set => SetProperty(ref isSearchReceiveCompany, value); }

        private bool isSearchSendCompany;
        public bool IsSearchSendCompany { get => isSearchSendCompany; set => SetProperty(ref isSearchSendCompany, value); }

        private bool isSearchTransportCompany;
        public bool IsSearchTransportCompany { get => isSearchTransportCompany; set => SetProperty(ref isSearchTransportCompany, value); }

        private Car currentCar = new Car();
        public Car CurrentCar { get => currentCar; set => SetProperty(ref currentCar, value); }

        private List<Car> cars = new List<Car>();
        public List<Car> Cars { get => cars; set => SetProperty(ref cars, value); }

        private Stuff currentStuff = new Stuff();
        public Stuff CurrentStuff { get => currentStuff; set => SetProperty(ref currentStuff, value); }

        private List<Stuff> stuffs = new List<Stuff>();
        public List<Stuff> Stuffs { get => stuffs; set => SetProperty(ref stuffs, value); }

        private CompanyEntiey currentReceiveCompany = new CompanyEntiey();
        public CompanyEntiey CurrentReceiveCompany { get => currentReceiveCompany; set => SetProperty(ref currentReceiveCompany, value); }

        private CompanyEntiey currentSendCompany = new CompanyEntiey();
        public CompanyEntiey CurrentSendCompany { get => currentSendCompany; set => SetProperty(ref currentSendCompany, value); }

        private CompanyEntiey currentTransportCompany = new CompanyEntiey();
        public CompanyEntiey CurrentTransportCompany { get => currentTransportCompany; set => SetProperty(ref currentTransportCompany, value); }

        private List<CompanyEntiey> companies = new List<CompanyEntiey>();
        public List<CompanyEntiey> Companies { get => companies; set => SetProperty(ref companies, value); }

        public ICommand SearchCommand { get; }
        public ICommand DesignCommand { get; }
        public ICommand DeleteCommand { get; }
        public ICommand PrintCommand { get; }

        public ISession Session { get; }
        public ICarRepository CarRepository { get; }
        public IStuffRepository StuffRepository { get; }
        public ICompanyRepository CompanyRepository { get; }
        public IDataRepository DataRepository { get; }
        public IConfigService ConfigService { get; }
        public IPrintService PrintService { get; }

        public ReportViewModel(
            ISession session,
            ICarRepository carRepository,
            IStuffRepository stuffRepository,
            ICompanyRepository companyRepository,
            IDataRepository dataRepository,
            IConfigService configService,
            IPrintService printService) 
        {
            Session = session;
            CarRepository = carRepository;
            StuffRepository = stuffRepository;
            CompanyRepository = companyRepository;
            DataRepository = dataRepository;
            ConfigService = configService;
            PrintService = printService;

            SearchCommand = new RelayCommand(Search);
            DeleteCommand = new RelayCommand<Data>(Delete);
            DesignCommand = new RelayCommand(Design);
            PrintCommand = new RelayCommand<Data>(Print);

            Load();
           
        }

        private void Print(Data data)
        {
            PrintService.Print(data, ConfigService.SystemConfig.IsPreview);
        }

        private void Design()
        {
            DesignWindow designWindow = new DesignWindow(ConfigService);
            designWindow.Show();
        }

        private async void Delete(Data data)
        {
            var view = new Dialog("是否执行删除操作？", System.Windows.MessageBoxButton.YesNo);
            bool result = (bool)await DialogHost.Show(view, "RootDialog");
            if (result)
            {
                int count = DataRepository.Delete(data);
                if (count > 0)
                {
                    Session.Show("删除成功");
                    Search();
                }
            }
        }

        private void Search()
        {
            var startDate = DateTime.Parse(StartDate.Date.ToString());
            var endDate = DateTime.Parse(EndDate.AddDays(1).Date.ToString());
            var list = DataRepository.GetAll().FindAll(t=>t.InsertDate >= startDate && t.InsertDate <= endDate);

            if(IsSearchCar)
            {
                list = list.FindAll(t => t.CarName == CurrentCar.Name);
            }

            if (IsSearchStuff)
            {
                list = list.FindAll(t => t.StuffName == CurrentStuff.Name);
            }

            if (IsSearchReceiveCompany)
            {
                list = list.FindAll(t => t.ReceiveCompanyName == CurrentReceiveCompany.Name);
            }

            if (IsSearchSendCompany)
            {
                list = list.FindAll(t => t.SendCompanyName == CurrentSendCompany.Name);
            }

            if (IsSearchTransportCompany)
            {
                list = list.FindAll(t => t.TransportCompanyName == CurrentTransportCompany.Name);
            }

            DataList = list;
        }

        public void Load()
        {
            Cars = CarRepository.GetAll();
            Stuffs = StuffRepository.GetAll();
            Companies = CompanyRepository.GetAll();
        }

    }
}
